﻿using System;
using System.Text;
using System.Xml;
using System.Net;
using System.IO;
using System.Windows.Forms;

namespace NewPagePatroller
{
    class CentralStuff
    {
        public readonly string _API_ = "http://en.wikipedia.org/w/api.php";
        public readonly string _INDX_ = "http://en.wikipedia.org/w/index.php";
        public readonly string _AGNT_ = "NewPagePatroller/1.0 (+http://en.wikipedia.org/wiki/User:Stwalkerster; Trident; .NET CLR;)";

        public readonly string _CSS_ = "<link rel=\"stylesheet\" href=\"http://en.wikipedia.org/skins-1.5/common/shared.css?195xx\" type=\"text/css\" media=\"screen\" /><link rel=\"stylesheet\" href=\"http://en.wikipedia.org/skins-1.5/monobook/main.css?195xx\" type=\"text/css\" media=\"screen\" /><link rel=\"stylesheet\" href=\"http://en.wikipedia.org/skins-1.5/monobook/IE70Fixes.css?195xx\" type=\"text/css\" media=\"screen\" /><link rel=\"stylesheet\" href=\"http://en.wikipedia.org/w/index.php?title=MediaWiki:Common.css&amp;usemsgcache=yes&amp;ctype=text%2Fcss&amp;smaxage=2678400&amp;action=raw&amp;maxage=2678400\" type=\"text/css\" /><link rel=\"stylesheet\" href=\"http://en.wikipedia.org/w/index.php?title=MediaWiki:Monobook.css&amp;usemsgcache=yes&amp;ctype=text%2Fcss&amp;smaxage=2678400&amp;action=raw&amp;maxage=2678400\" type=\"text/css\" /><link rel=\"stylesheet\" href=\"http://en.wikipedia.org/w/index.php?title=-&amp;action=raw&amp;maxage=2678400&amp;smaxage=0&amp;ts=20090121162115&amp;gen=css\" type=\"text/css\" /><link rel=\"stylesheet\" href=\"http://en.wikipedia.org/w/index.php?title=User:Stwalkerbot/monobook.css&amp;action=raw&amp;ctype=text/css\" type=\"text/css\" />";


        public string getRevisionText(string rvid, string title)
        {
            XmlTextReader rvReader = new XmlTextReader(
                sendWebRequest(_API_,
                "action=query&prop=revisions&rvprop=content&rvstartid="
                + rvid + "&rvdir=older&titles=" + title + "&rvlimit=1&format=xml"
                , HttpMethod.HTTP_GET).GetResponseStream());

            rvReader.Read();
            while (rvReader.Name != "rev") { rvReader.Read(); }

            return rvReader.ReadContentAsString();
        }


        public CookieContainer cookieJar;
        public RecentChange currentChange;
        public System.Collections.Queue RecentChanges;


        public enum HttpMethod
        {
            HTTP_GET = 1,
            HTTP_POST = 2
        }

        public HttpWebResponse sendWebRequest(string uri, string parameterString, HttpMethod method)
        {
            
            switch (method)
            {
                case HttpMethod.HTTP_GET:
                    try
                    {
                        HttpWebRequest getRequest = (HttpWebRequest)HttpWebRequest.Create(uri + "?" + parameterString);
                        getRequest.UserAgent = _AGNT_;
                        getRequest.Method = "GET";
                        getRequest.CookieContainer = cookieJar;
                        getRequest.Timeout = 5 * 1000;

                        HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();
                        return getResponse;
                    }
                    catch (WebException ex)
                    {
                        MessageBox.Show("Error: " + ex.Message);
                        return null;
                    }
                   
                case HttpMethod.HTTP_POST:
                    try
                    {
                        HttpWebRequest postRequest = (HttpWebRequest)HttpWebRequest.Create(uri + "?" + parameterString);
                        byte[] bytes = Encoding.ASCII.GetBytes(parameterString);
                        postRequest.ContentLength = bytes.Length;
                        postRequest.UserAgent = _AGNT_;
                        postRequest.Method = "POST";
                        postRequest.CookieContainer = cookieJar;
                        postRequest.Timeout = 5 * 1000;

                        Stream str = postRequest.GetRequestStream();
                        str.Write(bytes, 0, bytes.Length);
                        str.Close();

                        HttpWebResponse postResponse = (HttpWebResponse)postRequest.GetResponse();
                        postResponse.Cookies = postRequest.CookieContainer.GetCookies(postRequest.RequestUri);

                        cookieJar = postRequest.CookieContainer;

                        return postResponse;
                    }
                    catch (WebException ex)
                    {
                        MessageBox.Show("Error: " + ex.Message);
                        return null;
                    }
                default:
                    throw new ArgumentOutOfRangeException("Unrecognised HTTP method");
                  
            }
        }

        public string encodeToken(string token)
        {
            string newToken = "";
            foreach (char x in token)
            {
                if (x == '+')
                {
                    newToken += "%2B";
                }
                else if (x == '\\')
                {
                    newToken += "%5C";
                }
                else
                {
                    newToken += x;
                }
            }

            return newToken;
        }

    }

    public class RecentChange
    {
        public RecentChange() { }

        public string ns;
        public string title;
        public string rcid;
        public string user;
        public string timestamp;
        public string comment;
        public string patroltoken;

    }
}
